
package com.cn.wanxi.util;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Tool {

    /**
     * 根据传递过来的页数，判断是否正确
     *
     * @param value
     * @return
     */
    public static Integer getPage(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 1;
    }

    public static Integer getLimit(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return 10;
    }

    public static Integer strToInt(String value) {
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {

        }
        return -1;
    }

    /**
     * 利用MD5进行加密
     */
    public static String EncoderByMd5(String str) {
        //确定计算方法
        MessageDigest md5 = null;
        String newstr = "";
        try {
            md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            //加密后的字符串
            newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return newstr;
    }
}
